<?php

namespace App\other;

class UtilsClass
{
    public static function getRandomStringCode($max = 4){
        $code = "ABCDEFGHIJKLMNOPQRSTUVWXZYabcoefghijklmnopqrstuvwxyz0123456789";
        $backStr = '';
        for ($i = 0; $i < $max; $i++) {
            $x =  rand(0, strlen($code)-1);
            $backStr .=$code[$x];
        }
        return $backStr;
    }

    public static function getRandomNumberCode($max = 4){
        $key = "";
        $pattern='1234567890';
        for($i=0;$i<$max;$i++){
            $key .= $pattern{mt_rand(0,strlen($pattern)-1)};
        }
        return $key;
    }

    /**
     * @param array $array
     * @return string
     */
    public static function arrayToStringForItem(Array $array){
        $ermsg = ' ';
        foreach ($array as $key => $msg){
            $ermsg = $ermsg.$msg[0].' 
            ';
        }
        return $ermsg;
    }

    /**
     * 将字符串转换为float类型
     * @param string $str   原始字符
     * @param int $to       精确几位
     * @return float        返回值
     */
    public static function returnStringDouble($str, $to)
    {
        $double = floatval($str);
        return round($double, $to);
    }
}